package service

import (
	"perfect-admin/dao"
	"perfect-admin/model"
	"perfect-admin/model/request"
)

func GetFileList(queryInfo request.QueryInfo) (files []model.SysFile, total int, err error) {
	if queryInfo.Query == ""{
		if err = dao.DB.Model(&model.SysFile{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&files).Error
		return
	}
	if err = dao.DB.Model(&model.SysFile{}).Where(queryInfo.KeyWord + " like ?","%" + queryInfo.Query + "%").Count(&total).Error; err != nil {
		return
	}
	err = dao.DB.Where(queryInfo.KeyWord + " like ?", "%" + queryInfo.Query + "%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&files).Error
	return
}

func FileRename(file request.FileRename) (err error){
	err = dao.DB.Debug().Model(model.SysFile{}).Where("id = ?", file.ID).Update("name", file.NewFileName).Error
	return
}
